<?php

use common\components\Migration;
use common\modules\wechat\models\Account;
use common\modules\wechat\models\Module;
use common\modules\wechat\models\MpUser;

class m150217_131753_init_data extends Migration
{

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->initWechatTable();
        $this->initModuleTable();
        $this->initReplyRuleTable();
        $this->initUserTable();
        $this->initFansTable();
        $this->initHistoryTable();
        $this->initMediaTable();
    }

    /**
     * 公众号表
     */
    public function initWechatTable()
    {
        $rows = [
            [
                'name' => '测试公众号1',
                'account' => 'tourze-test-1',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号2',
                'account' => 'tourze-test-2',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE_VERIFY,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号3',
                'account' => 'tourze-test-3',
                'original' => 'gh0001',
                'type' => Account::TYPE_SUBSCRIBE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test3@tourze.com',
            ],
        ];

        foreach ($rows as $row)
        {
            $model = new Account;
            $model->setAttributes($row);
            if ( ! $model->save())
            {
                //print_r($model->errors);die();
            }
        }
    }

    /**
     * 扩展模块表
     */
    public function initModuleTable()
    {
        $rows = [
            /*
            [
                'id' => 'text',
                'name' => '文字回复',
                'type' => Module::TYPE_CORE,
                'category' => Module::CATEGORY_SYSTEM,
                'version' => '1.0',
                'ability' => '一问一答的文字对话',
                'description' => '一问一答的文字对话. 当访客的对话语句中包含指定关键字, 或对话语句完全等于特定关键字, 或符合某些特定的格式时. 系统自动应答设定好的回复内容.',
                'author' => 'tourze',
                'site' => 'http://www.tourze.com/',
                'admin' => 1,
                'migration' => 1,
                'reply_rule' => 1,
                'created_at' => time(),
                'updated_at' => time(),
                'created_by' => 0,
                'updated_by' => 0,
            ],
            [
                'id' => 'image',
                'name' => '图片回复',
                'type' => Module::TYPE_CORE,
                'category' => Module::CATEGORY_SYSTEM,
                'version' => '1.0',
                'ability' => '基本图片回复',
                'description' => '当访客的对话语句中包含指定关键字, 返回指定的图片',
                'author' => 'tourze',
                'site' => 'http://www.tourze.com/',
                'admin' => 1,
                'migration' => 1,
                'reply_rule' => 1,
                'created_at' => time(),
                'updated_at' => time(),
                'created_by' => 0,
                'updated_by' => 0,
            ],
            [
                'id' => 'voice',
                'name' => '音频回复',
                'type' => Module::TYPE_CORE,
                'category' => Module::CATEGORY_SYSTEM,
                'version' => '1.0',
                'ability' => '回复音频内容',
                'description' => '提供语音、音乐等音频类回复',
                'author' => 'tourze',
                'site' => 'http://www.tourze.com/',
                'admin' => 1,
                'migration' => 1,
                'reply_rule' => 1,
                'created_at' => time(),
                'updated_at' => time(),
                'created_by' => 0,
                'updated_by' => 0,
            ],
            [
                'id' => 'news',
                'name' => '图文回复',
                'type' => Module::TYPE_CORE,
                'category' => Module::CATEGORY_SYSTEM,
                'version' => '1.0',
                'ability' => '回复图文消息',
                'description' => '回复内容包括图片文字等生动的媒体内容. 当访客的对话语句中包含指定关键字, 或对话语句完全等于特定关键字, 或符合某些特定的格式时. 系统自动应答设定好的图文回复内容.',
                'author' => 'tourze',
                'site' => 'http://www.tourze.com/',
                'admin' => 1,
                'migration' => 1,
                'reply_rule' => 1,
                'created_at' => time(),
                'updated_at' => time(),
                'created_by' => 0,
                'updated_by' => 0,
            ],
            [
                'id' => 'api',
                'name' => '自定义接口回复',
                'type' => Module::TYPE_CORE,
                'category' => Module::CATEGORY_SYSTEM,
                'version' => '1.0',
                'ability' => '自定义处理逻辑',
                'description' => '可以选择远程处理或本地文件处理',
                'author' => 'tourze',
                'site' => 'http://www.tourze.com/',
                'admin' => 1,
                'migration' => 1,
                'reply_rule' => 1,
                'created_at' => time(),
                'updated_at' => time(),
                'created_by' => 0,
                'updated_by' => 0,
            ],
            */
        ];

        foreach ($rows as $row)
        {
            $model = new Module;
            $model->setAttributes($row);
            if ( ! $model->save())
            {
                echo $model->formatErrors() . "\n";
            }
        }
    }

    /**
     * 回复规则表
     */
    public function initReplyRuleTable()
    {
    }

    /**
     * 粉丝表
     */
    public function initFansTable()
    {
    }

    /**
     * 粉丝用户表
     */
    public function initUserTable()
    {
        $rows = [
            [
                'name' => '测试公众号1',
                'account' => 'tourze-test-1',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号2',
                'account' => 'tourze-test-2',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE_VERIFY,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号3',
                'account' => 'tourze-test-3',
                'original' => 'gh0001',
                'type' => Account::TYPE_SUBSCRIBE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test3@tourze.com',
            ],
        ];

        foreach ($rows as $row)
        {
            $model = new MpUser;
            $model->load($row, '');
            if ( ! $model->save())
            {
                //print_r($model->errors);die();
            }
        }
    }

    /**
     * 消息记录表
     */
    public function initHistoryTable()
    {
    }

    /**
     * 素材表
     */
    public function initMediaTable()
    {
    }
}
